Xbasic

Arithmetic Operators

Description

Arithmetic operators are used between numeric expressions to yield numeric results. Alpha Anywhere supports the following arithmetic operators:

Operator
Description
+

Addition

-

Subtraction

*

Multiplication

/

Division

^ or **

Exponentiation

()

Parentheses

Examples:

You can use Arithmetic operators in simple expressions involving just constants, for example:

? 15 + 3
= 18

? 15 - 5
= 10

? 15 - -5
= 20

? 2 * 3
= 6

You can also use Arithmetic operators in expressions involving fieldnames. For example, if Price contains 72.00 and Qty contains 2.0:

dim PRICE as N
dim QTY as N

PRICE = 72.00
QTY = 2.0

? PRICE * QTY
= 144

? PRICE * QTY * .9
= 129.6

If a character field or character expression contains a number, use the string-to-numeric conversion function, VAL, to convert it to a numeric value:

? VAL("25") * -3
= -75

Arithmetic Operator Precedence

An expression can include multiple arithmetic operators. Use parentheses to control the order in which the expression is evaluated. Alpha Anywhere uses the following order of precedence when evaluating expressions:

  1. Operations enclosed in parentheses

  2. Exponentiation (^ or **)

  3. Multiplication and division (*, /)

  4. Addition and subtraction (+, -)

Including operators in order according to their precedence allows parentheses to be implied when combining operations. For example, the expression 3 + 5 * 4 is equivalent to 3 + (5 * 4) since the multiplication operator has a higher precedence than the addition operator.

When multiple operations have the same precedence, they are evaluated from left to right. For example:

? 6 + 3 - 4
= 5

? (6 + 3) - 4
= 5

Exponentiation Operators

Name
Description
^ (Exponent Operator)

The ^ (exponent) operator raises Operand1 to the Operand2 power.

** (Exponent Operator)

The ** (exponent) operator raises Operand1 to the Operand2 power.